package com.amtf.datafile3.core.fil.parser;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;

import com.amtf.datafile3.DataException;
import com.amtf.datafile3.DataFieldParser;
import com.amtf.datafile3.core.std.type.DateField;

public class FilDateFieldParser extends AbsFilDataFieldParser<DateField, Date> {
	public FilDateFieldParser(int columnStartIndex, int length) {
		super(columnStartIndex, length, null);
	}

	public FilDateFieldParser(int columnStartIndex, int length, DateFormat format) {
		super(columnStartIndex, length, format);
	}

	@Override
	protected Date convertString(String value) throws DataException {
		if (value.length() == 0) {
			return null;
		}

		try {
			return ((DateFormat) format).parse(value);
		} catch (ParseException e) {
			throw new DataException("Exception when parse date value " + value, e);
		}
	}

	@Override
	public DataFieldParser clone() {
		FilDateFieldParser newObj = new FilDateFieldParser(columnStartIndex, length, (DateFormat) format);
		return newObj;
	}
}
